package com.ym.controller.management;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.exception.BusinessException;

import com.ym.medical.domain.entity.*;
import com.ym.medical.domain.param.DiseaseDeptParam;
import com.ym.medical.domain.param.SymptomDeptParam;
import com.ym.medical.domain.param.SymptomPartParam;
import com.ym.medical.service.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 基础数据管理
 */
@Api(tags = "基础数据管理")
@RestController
@RequestMapping("/management/baseData")
public class BaseDataManagement extends BaseController {

	@Autowired
	private IBaseDataSV baseDataSV;
	@Autowired
	private ISymptomPartSV symptomPartSV;
	@Autowired
	private IDiseaseDeptSV diseaseDeptSV;
	@Autowired
	private IStandardDeptSV standardDeptSV;
	@Autowired
	private IPositionListSV positionListSV;

	@ApiOperation("症状科室管理新增")
	@PostMapping("/symptomDept/add")
	public APIResponse<SymptomDeptEntity> insertSymptomDept(@ApiParam(name = "symptomDept", value = "症状科室关系实体", required = true)
													  @Validated(value = {SymptomDeptParam.Add.class}) @RequestBody SymptomDeptParam symptomDept) {
		SymptomDeptEntity dept = baseDataSV.insertSymptomDept(symptomDept);
		return APIResponse.success("症状科室管理新增成功", dept);
	}


	@ApiOperation("症状科室管理修改")
	@PostMapping("/symptomDept/update")
	public APIResponse<SymptomDeptEntity> updateSymptomDept(
			@ApiParam(name = "symptomDept", value = "症状科室关系实体", required = true)
			@Validated(value = {SymptomDeptParam.Update.class}) @RequestBody SymptomDeptParam symptomDept) {
		SymptomDeptEntity dept = baseDataSV.updateSymptomDept(symptomDept);
		return APIResponse.success("症状科室管理修改成功", dept);
	}

	@ApiOperation("症状科室管理删除")
	@PostMapping("/symptomDept/delete")
	public APIResponse deleteSymptomDept(
			@ApiParam(name = "ids", value = "症状科室关系实体id", required = true)
			@RequestBody List<Long> ids) {
		baseDataSV.deleteSymptomDept(ids);
		return successMsg("症状科室管理删除成功!");
	}

	@ApiOperation("症状科室管理展示")
	@GetMapping("/symptomDept/list")
	public APIResponse<PageInfo<SymptomDeptEntity>> listSymptomDept(
			@ApiParam(name = "symptomDept", value = "症状科室关系实体", required = true)
					SymptomDeptParam symptomDept,
			@RequestParam(required = false, defaultValue = "1") int pageNo,
			@RequestParam(required = false, defaultValue = "10") int limit) {
		PageInfo<SymptomDeptEntity> info = baseDataSV.listSymptomDept(symptomDept, pageNo, limit);
		return APIResponse.success("症状科室管理展示成功", info);
	}

	@ApiOperation("症状部位新增")
	@PostMapping("/symptomPart/add")
	public APIResponse<SymptomPartEntity> insertSymptomPart(
			@ApiParam(name = "symptomPart", value = "症状部位实体", required = true)
			@Validated(value = {SymptomPartParam.Add.class}) @RequestBody SymptomPartParam symptomPart) {
		//查询父级子级部位名称
		setPartNameByPartId(symptomPart);
		symptomPartSV.insert(symptomPart.covert());
		return APIResponse.success("症状部位新增成功", symptomPart.covert());
	}

	/**
	 * 设置父级子级部位名称
	 *
	 * @param symptomPart
	 */
	public void setPartNameByPartId(
			@RequestBody @Validated({SymptomPartParam.Add.class})
			@ApiParam(name = "symptomPart", value = "症状部位实体", required = true)
					SymptomPartParam symptomPart) {
		PositionListEntity positionList = positionListSV.selectById(symptomPart.getPartId());
		symptomPart.setPartName((positionList == null) ? null : positionList.getPlName());
		PositionListEntity subPositionList = positionListSV.selectById(symptomPart.getSubPartId());
		symptomPart.setSubPartName((subPositionList == null) ? null : subPositionList.getPlName());
	}

	@ApiOperation("症状部位修改")
	@PostMapping("/symptomPart/update")
	public APIResponse<SymptomPartEntity> updateSymptomPart(
			@ApiParam(name = "symptomPart", value = "症状部位实体", required = true)
			@Validated(value = {SymptomPartParam.Update.class}) @RequestBody SymptomPartParam symptomPart) {
		SymptomPartEntity part = symptomPartSV.selectById(symptomPart.getId());
		if (part == null) {
			throw new BusinessException("该症状部位记录不存在");
		}
		setPartNameByPartId(symptomPart);
		symptomPartSV.updateById(symptomPart.covert());
		return APIResponse.success("症状部位修改成功", symptomPart.covert());
	}

	@ApiOperation("症状部位删除")
	@PostMapping("/symptomPart/delete")
	public APIResponse updateSymptomPart(@ApiParam(name = "ids", value = "症状部位实体id", required = true) @RequestBody Long[] ids) {
		symptomPartSV.deleteByIds(ids);
		return successMsg("症状部位删除成功!");
	}

	@ApiOperation("症状部位展示")
	@GetMapping("/symptomPart/list")
	public APIResponse<PageInfo<SymptomPartEntity>> listSymptomPart(
			@ApiParam(name = "symptomPart", value = "症状部位实体", required = true)
					SymptomPartParam symptomPartModel,
			@RequestParam(required = false, defaultValue = "1") int pageNo,
			@RequestParam(required = false, defaultValue = "10") int limit) {
		PageInfo<SymptomPartEntity> info = symptomPartSV.listSymptomPart(symptomPartModel, pageNo, limit);
		return APIResponse.success("症状部位展示成功", info);
	}

	@ApiOperation("疾病科室展示")
	@GetMapping("/diseaseDepat/list")
	public APIResponse<PageInfo<DiseaseDeptEntity>> listDiseaseDepat(
			@ApiParam(name = "diseaseDept", value = "疾病科室实体", required = true)
					DiseaseDeptParam diseaseDeptModel,
			@RequestParam(required = false, defaultValue = "1") int pageNo,
			@RequestParam(required = false, defaultValue = "10") int limit) {
		PageInfo<DiseaseDeptEntity> info = diseaseDeptSV.listDiseaseDepat(diseaseDeptModel, pageNo, limit);
		return APIResponse.success("症状部位展示成功", info);
	}

	@ApiOperation("疾病科室增加")
	@PostMapping("/disease/add")
	public APIResponse<DiseaseDeptEntity> insertDiseaseDepat(
			@ApiParam(name = "diseaseDept", value = "疾病科室实体", required = true)
			@Validated(value = {DiseaseDeptParam.Add.class}) @RequestBody DiseaseDeptParam diseaseDept) {
		//查询父级部位名称
		diseaseDeptSV.insertDiseaseDepat(diseaseDept.covert());
		return APIResponse.success("疾病科室增加成功", diseaseDept.covert());
	}

	@ApiOperation("疾病科室修改")
	@PostMapping("/disease/update")
	public APIResponse<DiseaseDeptEntity> updateDiseaseDepat(
			@ApiParam(name = "diseaseDept", value = "疾病科室实体", required = true)
			@Validated(value = {DiseaseDeptParam.Update.class}) @RequestBody DiseaseDeptParam diseaseDept) {
		DiseaseDeptEntity dept = diseaseDeptSV.selectById(diseaseDept.getId());
		if (dept == null) {
			throw new BusinessException("该疾病科室不存在");
		}

		diseaseDeptSV.updateDiseaseDepat(diseaseDept.covert());
		return APIResponse.success("疾病科室修改成功", diseaseDept.covert());
	}

	@ApiOperation("疾病科室删除")
	@PostMapping("/disease/delete")
	public APIResponse deleteDiseaseDepat(@ApiParam(name = "ids", value = "疾病科室实体id", required = true) @RequestBody Long[] ids) {
		diseaseDeptSV.deleteByIds(ids);
		return successMsg("疾病科室删除成功");

	}


	@ApiOperation("查询科室列表 (type: 01-查询父级科室列表 02-根据父级科室code查询子级科室列表)")
	@GetMapping(value = "/stardDept/list")
	public APIResponse<PageInfo<StandardDeptEntity>> listStardDept(
			@RequestParam String type,
			String deptCode,
			@RequestParam(defaultValue = "1", required = false) int pageNo,
			@RequestParam(defaultValue = "10", required = false) int limit) {
		List<StandardDeptEntity> list = null;
		StandardDeptEntity model = new StandardDeptEntity();
		if ("01".equals(type)) {
			/**查询父级科室列表*/
			model.setDeptCode(deptCode);
			PageHelper.startPage(pageNo,limit);
			list = standardDeptSV.selectList(model);
		} else {
			model.setPartentDeptCode(deptCode);
			PageHelper.startPage(pageNo,limit);
			list = standardDeptSV.selectList(model);
		}
		return successPage(list);
	}

	@ApiOperation("查询父级部位列表 (type: 01-查询父级部位列表 02-根据父级部位id查询子级部位列表)")
	@GetMapping(value = "/part/list")
	public APIResponse<PageInfo<PositionListEntity>> listPart(@RequestParam String type,
                                                        Long id,
                                                        @RequestParam(defaultValue = "1", required = false) int pageNo,
                                                        @RequestParam(defaultValue = "10", required = false) int limit) {
		return successData(positionListSV.listPart(type, id, pageNo, limit));
	}
}
